package com.yociyy.gate.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.Optional;

/**
 * Swagger2处理器
 * 
 * @author Y.yang
 * @date 2020/5/28
 */
@Component
public class Swagger2Handler {

	@Autowired(required = false)
	private SecurityConfiguration securityConfiguration;

	@Autowired(required = false)
	private UiConfiguration uiConfiguration;

	private final SwaggerResourcesProvider swaggerResources;

	// https://blog.csdn.net/uaucome/article/details/85259654
	public Swagger2Handler(SwaggerResourcesProvider swaggerResources) {
		this.swaggerResources = swaggerResources;
	}

	public Mono<ServerResponse> securityConfiguration(ServerRequest serverRequest) {
		return ServerResponse.status(HttpStatus.OK)
				.contentType(MediaType.APPLICATION_JSON)
				.body(BodyInserters.fromValue(Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build())));
	}

	public Mono<ServerResponse> uiConfiguration(ServerRequest serverRequest) {
		return ServerResponse.status(HttpStatus.OK)
				.contentType(MediaType.APPLICATION_JSON)
				.body(BodyInserters.fromValue(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build())));

	}

	public Mono<ServerResponse> swaggerResources(ServerRequest serverRequest) {
		return ServerResponse.status(HttpStatus.OK)
				.contentType(MediaType.APPLICATION_JSON)
				.body((BodyInserters.fromValue((swaggerResources.get()))));

	}

}
